# 导入
import pygame
from plane_sprites import*
# 初始化
pygame.init()

# 游戏代码
# 页面
# set_mode((宽, 高))
screen = pygame.display.set_mode((480, 852))
# 背景
# 加载背景图
background = pygame.image.load('./images/bg.png')
# 显示图片 blit(图片变量, 位置)
screen.blit(background, (0, 0))


# 绘制英雄飞机
hero = pygame.image.load('./images/my_plane.png')
screen.blit(hero, (200, 600))

# 更新显示
pygame.display.update()
# 游戏循环

# 时钟对象
clock = pygame.time.Clock()

# 定义rect，记录飞机的初始位置
hero_rect = pygame.Rect(200, 600, 102, 126)

enemy = GameSprite("./images/enemy.png")
enemy1 = GameSprite("./images/enemy.png", 2)
enemy2 = GameSprite("./images/enemy.png", 3)
enemy3 = GameSprite("./images/enemy.png", 4)
enemy4 = GameSprite("./images/enemy.png", 5)
enemy_group = pygame.sprite.Group(enemy, enemy1, enemy2, enemy3, enemy4)

while True:
    # 指定循环内部代码的执行频率
    clock.tick(60)

    # 捕获事件
    # even_list = pygame.event.get()
    # if len(even_list) > 0:
    #     print(even_list)
    # 事件监听
    for event in pygame.event.get():
        # 是否退出游戏
        if event.type == pygame.QUIT:
            # print('yyds')
            pygame.quit()
            exit()


    # 修改飞机位置
    hero_rect.y -= 1

    # 判断飞机的位置
    if hero_rect.y <= 0:
        hero_rect.y = 852
    # 调bilt方法绘制
    screen.blit(background, (0, 0))
    screen.blit(hero, hero_rect)

    enemy_group.update()
    enemy_group.draw(screen)

    pygame.display.update()
    pass


